//
//  MessageImageShowVC.m
//  AirQuality
//
//  Created by Po on 15/9/18.
//  Copyright © 2015年 Po. All rights reserved.
//

#import "MessageImageShowVC.h"
#import "UIImageView+WebCache.h"

@interface MessageImageShowVC () <UIScrollViewDelegate>

@property (strong, nonatomic) UIScrollView * scrollView;
@property (strong, nonatomic) NSArray * images;
@property (strong, nonatomic) NSMutableArray * imageViewArray;

@end

@implementation MessageImageShowVC

- (instancetype)initWithImages:(NSArray *)images
{
    self = [super init];
    if (self) {
        _images = images;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor blackColor]];
    [self.view addSubview:[self getScrollView]];
    [self getAllImageView];
}

#pragma mark - event
- (void)tapTouch
{
    CGRect rect = CGRectMake(_imageContentRect.origin.x, _imageContentRect.origin.y, 80, 80);
    rect.origin.x += _imageCount % 3 * 90;
    rect.origin.y += _imageCount / 3 * 90;
    
    UIImageView * imageView = _imageViewArray[1];
    [imageView removeFromSuperview];
    CGRect imageRect = imageView.bounds;
    imageRect.origin.x = 0;
    imageRect.origin.y = (ScreenHeight - imageRect.size.height) / 2;
    [imageView setFrame:imageRect];
    [self.view addSubview:imageView];
    [_scrollView setHidden:YES];
    [self.view setBackgroundColor:[UIColor clearColor]];
    
    [UIView animateWithDuration:0.5 animations:^{
        [imageView setFrame:rect];
    } completion:^(BOOL finished) {
        [self.view removeFromSuperview];
        [self removeFromParentViewController];
    }];
}

#pragma mark - delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x == ScreenWidth) {
        if (_imageCount == 0) {
            [self exchangeImageWithDirection:DirectionTypeRight];
        } else if (_imageCount == _images.count - 1 && _images.count != 2) {
            [self exchangeImageWithDirection:DirectionTypeLeft];
        }
        return;
    } else if (scrollView.contentOffset.x < ScreenWidth) {
        if (_imageCount != 0) {
            [self exchangeImageWithDirection:DirectionTypeLeft];
        }
    } else {
        if (_imageCount != _images.count - 1) {
            [self exchangeImageWithDirection:DirectionTypeRight];
        }
    }
}

#pragma mark - function
- (void)exchangeImageWithDirection:(DirectionType)direction
{
    _imageCount += (direction == DirectionTypeLeft ? -1 : 1);
    for (NSUInteger i = 0 ; i < _imageViewArray.count ; i ++) {
        UIImageView * imageView = _imageViewArray[i];
        NSUInteger imageCount = _imageCount - (_imageCount == 0 ? 0 : 1) + i;
        imageCount = (imageCount >= _images.count ? _images.count - 1 : imageCount);
        [imageView sd_setImageWithURL:_images[imageCount] placeholderImage:nil];
    }
    if (_imageCount != 0 && _imageCount != (_images.count - 1)) {
        [_scrollView setContentOffset:CGPointMake(ScreenWidth, 0)];
    }
}

#pragma mark - getter
- (UIScrollView *)getScrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:ScreenBounds];
        [_scrollView setPagingEnabled:YES];
        [_scrollView setDelegate:self];
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTouch)];
        [tap setNumberOfTouchesRequired:1];
        [_scrollView addGestureRecognizer:tap];
    }
    return _scrollView;
}

- (void)getAllImageView
{
    _imageViewArray = [NSMutableArray array];
    NSUInteger maxNum = (_images.count > 3 ? 3 : _images.count );
    NSUInteger currentImageX = (_imageCount > 0 ? (_imageCount == _images.count - 1) ? 2 : 1 : 0);
    [_scrollView setContentOffset:CGPointMake(ScreenWidth * currentImageX, 0)];
    [_scrollView setContentSize:CGSizeMake(ScreenWidth * (maxNum >= 3 ? 3 : maxNum), ScreenHeight)];
    
    for (NSUInteger i = 0; i < maxNum; i ++) {
        CGRect rect = CGRectMake(ScreenWidth * i, 0, ScreenWidth, ScreenHeight);
        NSUInteger imageCount = _imageCount - (_imageCount == 0 ? 0 : 1) + i;
        imageCount = (imageCount >= _images.count ? _images.count - 1 : imageCount);
        
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:rect];
        [imageView sd_setImageWithURL:[NSURL URLWithString:_images[imageCount]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            CGSize imageSize = [RJTools reSetSize:image.size width:ScreenWidth];
            [imageView setBounds:CGRectMake(ScreenWidth * i, 0, imageSize.width, imageSize.height)];
            [imageView setCenter:CGPointMake(ScreenWidth / 2 + ScreenWidth * i, ScreenHeight / 2)];
        }];
        [_imageViewArray addObject:imageView];
        [_scrollView addSubview:imageView];
    }
}

@end
